var serverUrl = "http://localhost:8080";//服务器地址，like
var rootPath="/";//根路径,like /a11XX/


/**
 * get query parameter value
 * @param name parameter name
 * @returns {null} value
 */
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]);
    return null;
}
/**
 * get cookie
 * @param name
 * @returns
 */
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        return "";
    }
}

//jquery ajax 封装
/**
 * page ajax
 * @param {Object} url
 * @param {Object} data
 * @param {Object} async
 * @param {Object} type
 * @param {Object} successFunc
 * @param {Object} errorFunc
 */
var appAjax = function(url,data,async,type,successFunc,errorFunc){
    if(type == null){
        type="GET";
    }
    if(async == null){
        async = true;
    }
    if(data == null){
        data = {};
    }
    var processSuccess = function(data,status,xhr){
        if(data !=null){
            if(data.error!= null){
                var errorObj = data.error;
                var errorFirstType = errorObj.id.substr(0,1);
                switch(errorFirstType){
                    case "1":
                        switch(errorObj.id){
                            case "110001":
                                //do nothing
                                successFunc(data,status,xhr);
                                break;
                            case "110002":
                                //to login page
                                if(window.parent){
                                    window.parent.document.location.href=window.parent.document.location.href.replace("main.html","login.html");
                                }

                                break;
                            default:
                                if(typeof(alertBoxApp) != "undefined"){
                                    alertBoxApp.add({title:"错误编号："+ errorObj.id,
                                        content:"错误信息："+errorObj.mes,
                                        type:"error"});
                                    if(errorFunc){
                                        errorFunc();
                                    }
                                }else{
                                    console.error(errorObj.id+":"+errorObj.mes);
                                }
                                break;
                        }
                        break;
                    case "2":
                        console.error(errorObj.id+":"+errorObj.mes);
                        break;
                }
            }else{
                //针对两种类型数据进行处理
                //data:{error:null,obj:logicData};
                if(data.obj != null){
                    successFunc(data.obj,status,xhr);
                }else{
                    successFunc(data,status,xhr);
                }

            }

        }

    };
    /*	if(errorFunc == null){
            errorFunc = function(xhr,status){
                console.error(xhr);
            };
        }*/
    //data.sessionToken =  getCookie("sessionToken");

    $.ajax({
        url:serverUrl+rootPath+url,
        crossDomain:true,
        async:async,
        dataType:'json',
        type:type,
        data:data,
        success:processSuccess,
        error: function(xhr,status){
            console.error(xhr);
        }
    });
};


var fileUploadAjax =  function(url,data,fileElementId,secureuri,successFunc,errorFunc){
    //need jquery plugin :ajaxfileupload
    if(errorFunc == undefined){
        errorFunc = function(xhr,status){
            console.error(xhr);
        };
    }
    var newSuccessFunc = function(data,status,xhr){
        var json = $.parseJSON($(data).find("body").text());//把字符串转化为json对象
        //
        successFunc(json,status,xhr);
    };
    $.ajaxFileUpload({
        url:serverUrl+rootPath+url,
        secureuri: secureuri, //是否需要安全协议，一般设置为false
        fileElementId: fileElementId, //文件上传域的ID
        data:data,
        success: newSuccessFunc,
        error:errorFunc
    });
};

/**
 * 判断字段是否在数据库中存在
 */
var notInDB = function(str,url){
    var flag = false;
    appAjax(url+str,null,false,null,function(data){
        flag= data;
    },null);
    return flag;
};
/**
 * 判断是否为时间
 */
var isDate=function (str) {
    var date = str;
    var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);

    if (result == null)
        return false;
    var d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);
};
/**
 * 如果validator存在，添加判断数据库存在方法和时间判断方法
 */
if(typeof(validator)!="undefined"){
    validator.notInDB  = notInDB;
    validator.isDate  = isDate;
}